import axios from "axios"
import {Toast} from 'vant'
let last_show_error_time=0;
/**
 * 
 * @param {string} error 
 * @returns 
 */
let errorcallback_default=(error)=>{
    let n=new Date().getTime();
    // 限制最多10s内提示一次网络错误
    if(n+10*1000>last_show_error_time)return;
    last_show_error_time=n
    console.log("数据错误",error)

    Toast("网络错误");
}
let httpRequest=function(method,url,params,data,errorcallback=errorcallback_default){
    return axios({
        url:url,
        method:method,
        baseURL:"/api",
        headers:{
            "content-type":"application/json"
        },
        param:params,
        data:data,
        timeout:10000
    }).then(response=>{
        return response.data;
    }).catch(function(error){
        console.log("错误捕获")
        errorcallback(error);
    });
}

export default httpRequest